package com.tjetc.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tjetc.common.Result;
import com.tjetc.domain.Bed;
import com.tjetc.domain.Department;
import com.tjetc.domain.Record;
import com.tjetc.domain.Room;
import com.tjetc.service.*;
import com.tjetc.service.impl.*;
import com.tjetc.utils.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;

@WebServlet("/bed/*")
@MultipartConfig
public class BedServlet extends HttpServlet {
    private BedService bedService=new BedServiceImpl();
    private RoomService roomService=new RoomServiceImpl();
    private DepartmentService departmentService=new DepartmentServiceImpl();
    private DoctorService doctorService = new DoctorServiceImpl();
    private RecordService recordService = new RecordServiceImpl();
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        String methodName = uri.substring(uri.lastIndexOf("/") + 1);
        try {
            Method method = this.getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(this,request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String number=request.getParameter("number");
        String status1= request.getParameter("status");
        String curPage = request.getParameter("curPage");
        String ps = request.getParameter("pageSize");
        int status=0;
        if (status1==null||status1==""){
           status=2;
        }else{
            status=Integer.valueOf(status1);
        }
        number=number==null?"":number;
        //接受请求参数
        //空处理:赋值给当前页码
        int currentPage = curPage == null ? 1:Integer.valueOf(curPage);
        //每页显示的条数
        int pageSize = ps == null ? 5 : Integer.parseInt(ps);
        int count=0;
        if(status==2){
            //总记录数
            count =bedService.getCount(number);
        }else{
            count=bedService.getCount1(status,number);
        }
        PageInfo<Bed> pageInfo = new PageInfo<>(currentPage, count, pageSize);
        List<Bed> list=null;
        if (status==2){
            list=bedService.list(number,pageInfo);
        }else{
            list=bedService.list1(status,number,pageInfo);
        }
        pageInfo.setList(list);
        new ObjectMapper().writeValue(response.getOutputStream(),pageInfo);
    }


    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String number=request.getParameter("number");
        int roomid= Integer.parseInt(request.getParameter("roomid"));
        double price= Double.parseDouble(request.getParameter("price"));
        //查询该房间内绑定了几张床
        Room room=roomService.findById(roomid);
        int count=bedService.getRCount(roomid);
        Department department=departmentService.getById(room.getDepid());
        if (count==0){//房间里现在没有床位所以无需查询是否重名
                Bed bed=new Bed();
                bed.setNumber(number);
                bed.setDepid(department.getId());
                bed.setPrice(price);
                bed.setRoomid(roomid);
             boolean b=bedService.add(bed);
             if (b){
                 new ObjectMapper().writeValue(response.getOutputStream(), Result.success("添加成功，此价格成为该房间的统一价格"));
             }
             new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("添加失败"));
        }else if(0<count&&count<3){//根据roomid肯定已经存在一个或者两个床
            double price1=0;
            //查出来该房间内有没有床位号重复的
            Bed bed1=bedService.findByNumber(number,roomid);
            List<Bed> list=bedService.findByRid(roomid);
            for (Bed bed : list) {
                price1=bed.getPrice();
            }
            if (bed1==null){
                Bed bed=new Bed();
                bed.setNumber(number);
                bed.setDepid(department.getId());
                bed.setPrice(price1);
                bed.setRoomid(roomid);
                boolean b=bedService.add(bed);
                if (b){
                    new ObjectMapper().writeValue(response.getOutputStream(), Result.success("添加成功,价格已自动修改为该病房的统一价格"));
                }
                new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("添加失败"));
            }else{
                new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("添加失败，床位号重复！"));
            }
        }
        else if(count>=3){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("添加失败，该房间已有三张病床！"));
        }
    }


    protected void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int roomid= Integer.parseInt(request.getParameter("roomid"));
        double price= Double.parseDouble(request.getParameter("price"));
        Bed bed=new Bed();
        bed.setRoomid(roomid);
        bed.setPrice(price);
        boolean b=bedService.updatePrice(bed);
        if (b){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.success("修改成功,已修改为此病房统一价格"));
        }
        new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("修改失败"));
    }


    protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id= Integer.parseInt(request.getParameter("id"));
        System.out.println("id = " + id);
        Bed bed=bedService.findById(id);
        System.out.println("bed = " + bed);
        if (bed.getStatus()==1){
            new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("删除失败，该病床已被人占用"));
        }else{
            boolean b=bedService.del(bed);
            if (b){
                new ObjectMapper().writeValue(response.getOutputStream(), Result.success("删除成功"));
            }
            new ObjectMapper().writeValue(response.getOutputStream(), Result.fail("删除失败"));
        }
    }

    protected void listByDepid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer rid = Integer.valueOf(request.getParameter("rid"));
        Record record = recordService.getById(rid);
        int depid = doctorService.getDepidById(record.getDid());
        List<Bed> beds = bedService.findByDepid(depid);
        new ObjectMapper().writeValue(response.getOutputStream(),beds);
    }

    protected void bedInfoByUid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        Bed bed = bedService.findByUid(uid);
        new ObjectMapper().writeValue(response.getOutputStream(),bed);
    }

    protected void roomInfoByUid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        Bed bed = bedService.findByUid(uid);
        Room room = roomService.findById(bed.getRoomid());
        new ObjectMapper().writeValue(response.getOutputStream(),room);
    }
}
